#include "altmap.h"

#include "altapplication.h"
#include "altmapeditorplugin.h"
#include "altmapplugin.h"

#include <QtAlgorithms>

namespace Alt {
     QStringList Map::allMapTypes(){
          QStringList mapTypeList = Application::mapPlugins().keys();
          qSort(mapTypeList.begin(), mapTypeList.end());
          return mapTypeList;
     }

     QStringList Map::allEditableMapTypes(){
          QStringList mapTypeList = Application::mapEditorPlugins().uniqueKeys();
          qSort(mapTypeList.begin(), mapTypeList.end());
          return mapTypeList;
     }

     Map* Map::fromFile(const QString& fileName){
          QFile file(fileName);
          if(!file.open(QIODevice::ReadOnly)){
               return 0;
          }
          QDataStream stream(&file);
          return fromStream(stream);
     }

     Map* Map::fromStream(QDataStream& stream){
          QString type;
          stream >> type;
          MapPlugin* plugin = Application::findMapPlugin(type);
          Map* map = 0;
          if(plugin){
               map = plugin->createMap(type);
               if(!map->load(stream)){
                    delete map;
               }
          }
          return map;
     }

     Map::Map(QGraphicsItem* parent) : QGraphicsItem(parent) {
     }

     qreal Map::height() const {
          return this->boundingRect().height();
     }

     qreal Map::width() const {
          return this->boundingRect().width();
     }
}
